"""
施工大事记
文档通用
通用文档
"""
import pytest
from playwright.sync_api import Page, expect, Playwright
import allure
from Config.config import *
from Config.URL import *
from Config.AutoFlowEngine import autoflow
from Config.Base import Base
from Config.Tool import tool

# 测试用例数据
case_data = [{'caseid': '监理大事记', 'username': 'jianli1',
              'url': url_jianlidashiji}]  # {'caseid':'施工大事记','username':'shigong1', 'url':url_shigongdashiji},{'caseid':'档案管理制度','username':'admin', 'url':url_danganguanlizhidu}


@pytest.mark.业务系统
@pytest.mark.综合管理
@pytest.mark.parametrize('case', case_data)
def test_case(case) -> None:
    username = case['username']
    officeDocumentsUrl = case['url']

    # headless=False
    SetupTeardown = Base(playwright)
    SetupTeardown.goto(url)
    SetupTeardown.login(username_admin, password_admin)
    context = SetupTeardown.context
    page = SetupTeardown.page

    page.goto(url + officeDocumentsUrl)
    page.wait_for_timeout(500)
    if page.locator("#app").get_by_text("分类管理").is_visible():
        if page.get_by_role("tree").get_by_text("AutoTest-分类").is_visible():
            page.get_by_role("tree").get_by_text("AutoTest-分类").click()
        else:
            page.locator(".el-tooltip").first.click()
            page.get_by_role("dialog").get_by_role("button", name=" 新建").click()
            page.get_by_placeholder("请输入当前分类名称").fill("AutoTest-分类")
            page.get_by_role("button", name="确 定").click()
            expect(page.get_by_role("alert")).to_contain_text("新建成功！")
            page.get_by_label("close 分类管理").click()
            page.locator("#app").get_by_text("AutoTest-分类").click()

    # page.get_by_placeholder("请选择标段").click()
    # page.locator("ul").filter(has_text=re.compile(r"^应急供水工程施工监理标段$")).locator("span").click()
    page.get_by_role("button", name=" 新建").click()
    title = tool.nowtime(Name="AutoTest通用文档")
    page.locator("form").filter(has_text="基础信息标题").get_by_placeholder("请输入标题").fill(title)
    page.get_by_placeholder("请输入描述").fill("AutoTest描述")
    page.get_by_role("button", name="+ 新建").click()
    with page.expect_file_chooser() as fc_info:
        page.get_by_role("button", name="上传文件").click()
    file_chooser = fc_info.value
    file_chooser.set_files(test_datas_dir + "\用户导入模板.xlsx")
    allure.step("等待上传完成")
    page.wait_for_timeout(2000)
    page.get_by_role("button", name="确定").click()

    page.get_by_role("button", name="+ 新建").click()
    page.get_by_role("switch").locator("span").click()
    with page.expect_file_chooser() as fc_info:
        page.get_by_role("button", name="上传文件").click()
    file_chooser = fc_info.value
    file_chooser.set_files(test_datas_dir + "\签章.pdf")
    allure.step("等待上传完成")
    page.wait_for_timeout(2000)
    page.get_by_role("button", name="确定").click()

    if page.get_by_role("button", name="发起流程").is_visible():
        page.get_by_role("button", name="发起流程").click()
        page.get_by_placeholder("请选择下一审批人").click()
        page.get_by_title("谭监").click()
        page.get_by_role("button", name="确 定").click()
        page.get_by_role("button", name="确定").click()
        expect(page.get_by_role("alert")).to_contain_text("流程发起成功")
        page = autoflow(SetupTeardown, title)

        page.close()
        context = browser.new_context(viewport={'width': 1920, 'height': 1080})
        page = context.new_page()
        page.goto(url + url_login)
        page.get_by_placeholder("请输入用户名").fill(username)
        page.get_by_placeholder("请输入密码").fill(password_admin)
        page.get_by_role("button", name="登 录").click()

        page.goto(url + officeDocumentsUrl)
        page.wait_for_timeout(1000)
    elif page.get_by_role("button", name="确 定").is_visible():
        page.get_by_role("button", name="确 定").click()
        expect(page.get_by_role("alert")).to_contain_text("新建成功！")

    # page.get_by_role("textbox", name="请输入标题").fill(title)
    page.locator("form").filter(has_text="标题查询清空").get_by_placeholder("请输入标题").fill(title)
    page.get_by_role("button", name=" 查询").click()
    page.wait_for_timeout(1000)
    page.get_by_text(title, exact=True).click()
    page.get_by_role("button", name="取 消").click()
    page.get_by_role("button", name=" 版本更新").click()
    page.wait_for_timeout(1000)

    if page.get_by_role("button", name="发起流程").is_visible():
        page.get_by_role("button", name="发起流程").click()
        page.get_by_placeholder("请选择下一审批人").click()
        page.get_by_title("谭监").click()
        page.get_by_role("button", name="确 定").click()
        page.get_by_role("button", name="确定").click()
        expect(page.get_by_role("alert")).to_contain_text("流程发起成功")
        page = autoflow(SetupTeardown, title)
    elif page.get_by_role("button", name="确 定").is_visible():
        page.get_by_role("button", name="确 定").click()
        expect(page.get_by_role("alert")).to_contain_text("更新成功！")

    SetupTeardown.close()
